<?php

/**
 * LongTermProgram form.
 *
 * @package    sf_sandbox
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class LongTermProgramForm extends BaseLongTermProgramForm {

    public function configure() {
        $this->widgetSchema['start_date'] = new sfHijreeDateWidget();
        $this->widgetSchema['end_date'] = new sfHijreeDateWidget();
        $this->widgetSchema['course_id']->setOption('add_empty',('select'));
        $objs = $this->getObject()->getLongProgramCondition();

        $posted_conditions = $this->count_post_conditions();

        if ($objs instanceof Doctrine_collection && count($objs) > 0) {
            foreach ($objs as $k => $one) {
                $this->embedForm('condition_' . ($k + 1), new LongProgramConditionForm($one));
                $this->widgetSchema['condition_' . ($k + 1)]->setLabel(__('Condition').' '.($k + 1));
            }

            $conditions = count($objs);
            if ($posted_conditions > $conditions) {
                while ($posted_conditions != $conditions) {
                    $k++;
                    $conditions++;
                    $this->embedForm('condition_' . ($k + 1), new LongProgramConditionForm());
                    $this->widgetSchema['condition_' . ($k + 1)]->setLabel(__('Condition').' '.($k + 1));
                }
            }
        } else {
            if (sfContext::getInstance()->getRequest()->getMethod() == 'POST') {
                $post_params = sfContext::getInstance()->getRequest()->getPostParameters();
                $c = 1;
                foreach ($post_params['long_term_program'] as $k => $one) {
                    if (strpos($k, 'condition') !== false) {
                        $this->embedForm('condition_' . ($c), new LongProgramConditionForm());
                        $this->widgetSchema['condition_' . $c]->setLabel(__('Condition').' '.($c));
                        $c++;
                    }
                }
            } else {
                $this->embedForm('condition_1', new LongProgramConditionForm());
                $this->widgetSchema['condition_1']->setLabel(__('Condition').' '.'1');
            }
        }
        $this->widgetSchema['start_date']->setLabel('application start date');
        $this->widgetSchema['end_date']->setLabel('application end date');
    }

    private function count_post_conditions() {

        $post_params = sfContext::getInstance()->getRequest()->getPostParameters();
        if (is_array($post_params) && count($post_params)) {
            $c = 0;
            foreach ($post_params['long_term_program'] as $k => $one) {
                if (strpos($k, 'condition') !== false) {
                    $c++;
                }
            }
            return $c;
        }
        return false;
    }

    public function saveEmbeddedForms($con = null, $forms = null) {
        if (null === $con) {
            $con = $this->getConnection();
        }

        if (null === $forms) {
            $forms = $this->embeddedForms;
        }

        foreach ($forms as $form) {
            if ($form instanceof sfFormObject) {
                $form->saveEmbeddedForms($con);
                $form->getObject()->set('long_program_id',$this->getObject()->getId());
                $form->getObject()->save($con);
            } else {
                $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
            }
        }
    }

}
